package com.example.runner;

import com.example.domain.entity.Plant;
import com.example.service.IPlantService;
import com.example.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
/** 启动时预处理，将所有植物的关注量存入redis */
//@Component
public class FollowerNumOfPlantRunner implements CommandLineRunner {
    @Autowired
    RedisCache redisCache;

    @Autowired
    IPlantService plantService;


    @Override
    public void run(String... args) throws Exception {
        List<Plant> plantList=plantService.list();
        HashMap<String,Long> plantFollowerNumMap=new HashMap<>();

        for(Plant plant:plantList){
            plantFollowerNumMap.put(plant.getPlantId().toString(),plant.getFollowerNum());
        }

        redisCache.setCacheMap("PlantFollowerNum",plantFollowerNumMap);

    }
}
